package com.ddstation.crm.menu.controller;

import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ddstation.crm.login.bean.SessionLoginData;
import com.ddstation.crm.login.util.WnSessionUtil;
import com.ddstation.crm.menu.service.MENU_FILTER_ENUM;
import com.ddstation.crm.menu.service.MenuService;

/**
 * 《健康管理平台》 菜单
 * @author zhangdifeng
 * @date 2017年7月12日
 */
@Controller
public class MainMenuController {

	/**
	 * 纵向菜单
	 * @param session
	 * @param model
	 * @return
	 * @throws IllegalAccessException
	 */
	@RequestMapping(value="/main.do")
	public String initMainMenuEvent(
			@RequestParam(required=false)String initPageUrl, // 打开的页面url
			@RequestParam(required=false)String initPageTitle, // 打开的页面title
			@RequestParam(required=false)String srvUserUUID, // 默认服务的user 横向菜单服务only
			@RequestParam(required=false)Integer workFlowType, // 默认服务 横向菜单服务only
			@RequestParam(required=false)String townId, // 地区id
			@RequestParam(required=false)Integer today, // 0：今日  1：本周 10全部
			@RequestParam(required=false)Integer status, //9:全部 0：正常  -1：异常
			@RequestParam(required=false)String otherData, //其他参数 格式【名:值;值:名】
			HttpSession session,
			Model model) throws IllegalAccessException {
		
		// 从session获取基础数据
		SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
		if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
			return "forward:login?timeout=true"; // session 过期
		}

		//跳转特定title
		if (StringUtils.isNotBlank(initPageTitle)) {
			model.addAttribute("initPageTitle", initPageTitle);
		}

		//查询特定地区数据传递地区id
		if (StringUtils.isNotBlank(townId)) {
			model.addAttribute("townId", townId);
		}
		// 0：今日  1：本周 10:全部
		if (today != null) {
			model.addAttribute("today", today);
		}
		// 9:全部 0：正常  -1：异常
		if (status != null) {
			model.addAttribute("status", status);
		}
		// 其他参数 格式【名:值;值:名】
		if (otherData != null) {
			model.addAttribute("otherData", otherData);
		}

//        model.addAttribute("jurisdictionMenus", loginData.getJurisdictionMenus());
		model.addAttribute("jurisdictionMenus", loginData.getWorkbenchMenus());

		return "menu/main_menu";
	}
	
	/**
	 * 纵向菜单
	 * @param session
	 * @param model
	 * @return
	 * @throws IllegalAccessException
	 */
//	@RequestMapping(value="wncrm_menu.do/load", method = RequestMethod.POST)
//	public @ResponseBody Map<String, Object> loadMainMenuEvent(HttpSession session) {
//		// 从session获取登录用户基础数据
//		String institutionId = WnSessionUtil.getInstitutionId(session);
//		Integer supervisorId = WnSessionUtil.getSupervisorId(session);
//		Map<String, Object> retMap = new HashMap<String, Object>();
//		retMap.put("menus", menuService.getVerticalMenu(institutionId, supervisorId, MENU_FILTER_ENUM.INSTITUTION_AND_DOCTOR));
////		retMap.put("superManager", WnSessionUtil.isSuperManager(session));
//		retMap.put("errorcode", 0);
//		return retMap;
//	}
	
//	/**
//	 * 菜单设置
//	 * @param session
//	 * @return
//	 */
//	@RequestMapping(value="wncrm_menu.do/settting/init", method = RequestMethod.POST)
//	public @ResponseBody Map<String, Object> getHorizontalMenuSettingEvent(
//			HttpSession session) {
//
//		// 从session获取登录用户基础数据
//		String institutionId = WnSessionUtil.getInstitutionId(session);
//		Integer doctorId = WnSessionUtil.getDoctorId(session);
//		int hcrmAccess = WnSessionUtil.hcrmAccess(session);
//		Map<String, Object> retMap = new HashMap<String, Object>();
//		retMap.put("errcode", 0);
//		retMap.put("menus", menuService.initInstitutionVerticalMenuSetting(institutionId, doctorId));
//		retMap.put("hcrmaccess", hcrmAccess); // HCRM使用状况
//		return retMap;
//	}
	
//	/**
//	 * 菜单设置
//	 * @param session
//	 * @return
//	 */
//	@RequestMapping(value="wncrm_menu.do/settting/save", method = RequestMethod.POST)
//	public @ResponseBody Map<String, Object> saveHorizontalMenuSettingEvent(
//			@RequestBody List<DtHealthManageInstitutionMenuCustomizeEntity> rules,
//			HttpSession session) {
//
//		// 从session获取登录用户基础数据
//		String institutionId = WnSessionUtil.getInstitutionId(session);
//		Map<String, Object> retMap = new HashMap<String, Object>();
//		menuService.saveInstitutionVerticalMenuSetting(institutionId, rules);
//		retMap.put("errcode", 0);
//		return retMap;
//	}
	
//	private static final Logger logger = LoggerFactory.getLogger(MainMenuController.class);
	@Inject private MenuService menuService = null;
}
